package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.common.exception.CustException;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {

    // 查询
    @Override
    public ResponseResult findChannelByName(ChannelDTO channelDTO) {
        // 判断参数是否为空
        if (channelDTO == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 分页检查
        channelDTO.checkParam();
        //分页
        Page page = new Page(channelDTO.getPage(), channelDTO.getSize());
        // 根据条件查找用户
        LambdaQueryWrapper<AdChannel> wrapper = Wrappers.<AdChannel>lambdaQuery();
        // 判断前端是否穿了name值
        if (StringUtils.isNotBlank(channelDTO.getName())){
            wrapper.like(AdChannel::getName,channelDTO.getName()); // 关键字查询
        }
        // 排序
        wrapper.orderByAsc(AdChannel::getOrd); // 根据顺序编号升序排序
        // 调用方法
        IPage iPage = this.page(page, wrapper);
        // 响应
        return new PageResponseResult(channelDTO.getPage(), channelDTO.getSize(),iPage.getTotal(),iPage.getRecords());
    }

    // 添加
    @Override
    public ResponseResult insertChannel(AdChannel adChannel) {
        // 参数校验
        if (adChannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID); // 前端传输的值是空的
        }
        // 名称校验
        if (StringUtils.isBlank(adChannel.getName())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"名字不能为空");
        }
        // 长度校验
        if (adChannel.getName().length() > 10){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"名称长度不能超过10");
        }
        // 查看数据库是否有此频道
        LambdaQueryWrapper<AdChannel> wrapper = Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, adChannel.getName());
        int count = this.count(wrapper);
        if (count > 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"频道已存在");
        }
        adChannel.setCreatedTime(new Date());
        // 添加用户
        this.save(adChannel);
        return ResponseResult.okResult();
    }

    // 修改
    @Override
    public ResponseResult updateChannel(AdChannel adChannel) {
        // 校验参数
        // 判断前端传的值是否为空
        if (adChannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 判断ID字段是否为空
        if (adChannel.getId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"ID不能为空");
        }
        // 根据ID查询用户
        AdChannel channel = this.getById(adChannel.getId());
        if (channel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道不存在");
        }
        // 判断长度是否合格
        if (adChannel.getName().length() > 10){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名称长度不能超过10");
        }

        // 判断名称是否重复
        if (StringUtils.isBlank(adChannel.getName()) && !channel.getName().equals(adChannel.getName())){
            int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, adChannel.getName()));
            if (count > 0){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"频道已存在");
            }
        }
        this.updateById(adChannel);
        return ResponseResult.okResult();
    }

    // 删除
    @Override
    public ResponseResult deleteById(Integer id) {
        // 校验参数
        // 判断ID是否为空
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"ID不能为空");
        }
        // 根据ID查询用户
        AdChannel adChannel = this.getById(id);
        if (adChannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"频道不存在");
        }
        // 判断用户状态
        if (adChannel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道使用中");
        }

        // 异常捕获
//        int i = 1/0;

        // 自定义异常类
//        if(true){
//            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST,"这是一个自定义的异常类");
//        }
        // 执行方法
        this.removeById(id);
        // 响应
        return ResponseResult.okResult();
    }
}
